<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>支付模拟页面</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 500px;
            margin: 0 auto;
            padding: 20px;
            text-align: center;
        }
        .container {
            border: 1px solid #e8e8e8;
            border-radius: 8px;
            padding: 20px;
            margin-top: 50px;
        }
        h1 {
            color: #1890ff;
        }
        .order-info {
            margin: 20px 0;
            text-align: left;
        }
        .info-item {
            margin-bottom: 10px;
        }
        .amount {
            font-size: 24px;
            color: #f5222d;
            margin: 20px 0;
        }
        .button-group {
            display: flex;
            justify-content: space-around;
            margin: 30px 0;
        }
        .btn {
            padding: 10px 20px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 16px;
        }
        .success-btn {
            background-color: #52c41a;
            color: white;
        }
        .fail-btn {
            background-color: #f5222d;
            color: white;
        }
        .result {
            margin-top: 20px;
            padding: 10px;
            border-radius: 4px;
            display: none;
        }
        .success {
            background-color: #f0f9eb;
            color: #52c41a;
            display: block;
        }
        .fail {
            background-color: #fff2f0;
            color: #f5222d;
            display: block;
        }
        .back-link {
            margin-top: 20px;
            display: inline-block;
            color: #1890ff;
            text-decoration: none;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>订单支付</h1>
        <div class="order-info">
            <div class="info-item">订单ID: <span id="orderId"></span></div>
            <div class="info-item">创建时间: <span id="createTime"></span></div>
            <div class="info-item">商品: <span id="productName"></span></div>
        </div>
        <div class="amount">支付金额: ¥<span id="amount"></span></div>
        <div class="button-group">
            <button class="btn success-btn" id="paySuccess">支付成功</button>
            <button class="btn fail-btn" id="payFail">支付失败</button>
        </div>
        <div class="result" id="successResult">
            <h2>✓ 支付成功</h2>
            <p>您的订单已支付完成，会员权益已生效</p>
        </div>
        <div class="result" id="failResult">
            <h2>✗ 支付失败</h2>
            <p>支付未完成，请返回应用重试</p>
        </div>
        <a href="javascript:history.back()" class="back-link">返回应用</a>
    </div>

    <script>
        // 从URL获取参数
        function getQueryParams() {
            const params = {};
            const queryString = window.location.search.substring(1);
            const pairs = queryString.split('&');
            for (let i = 0; i < pairs.length; i++) {
                const pair = pairs[i].split('=');
                params[pair[0]] = decodeURIComponent(pair[1] || '');
            }
            return params;
        }

        // 初始化页面
        function initPage() {
            const params = getQueryParams();
            const orderId = params.orderId;
            const timestamp = params.timestamp;
            const signature = params.signature;

            if (!orderId || !timestamp || !signature) {
                alert('无效的支付链接');
                return;
            }

            // 显示订单ID
            document.getElementById('orderId').textContent = orderId;

            // 显示当前时间作为创建时间
            const now = new Date();
            document.getElementById('createTime').textContent = now.toLocaleString();

            // 假设获取订单详情（在实际应用中，这里应该调用API获取订单详情）
            // 为了简化，我们假设订单金额和商品名称
            document.getElementById('productName').textContent = '会员服务';
            document.getElementById('amount').textContent = '25.00'; // 默认金额，实际应从API获取

            // 添加支付按钮事件
            document.getElementById('paySuccess').addEventListener('click', function() {
                // 调用支付回调接口，模拟支付成功
                const callbackUrl = `http://localhost:4002/api/vip/pay-callback?orderId=${orderId}&signature=${signature}&timestamp=${timestamp}`;
                fetch(callbackUrl)
                    .then(response => {
                        if (response.ok) {
                            document.getElementById('successResult').classList.add('success');
                            document.getElementById('button-group').style.display = 'none';
                        } else {
                            alert('支付处理失败，请重试');
                        }
                    })
                    .catch(error => {
                        console.error('支付失败:', error);
                        alert('支付处理失败，请重试');
                    });
            });

            document.getElementById('payFail').addEventListener('click', function() {
                // 显示支付失败
                document.getElementById('failResult').classList.add('fail');
            });
        }

        // 页面加载完成后初始化
        window.onload = initPage;
    </script>
</body>
</html>